﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{

    public partial class HR_EmpChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                FormInfo.DepartMentBind(Drp_DEPMET);
                LoadData();
            }
        }

        public void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["empid"]))
            {
                id = int.Parse(Request["empid"]);
                HR_EMP emp = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.ID == id).ToFirst();
                if (emp.BIRDAT.HasValue)
                    txt_BIRDAT.Text = emp.BIRDAT.Value.ToString("yyyy-MM-dd");
                txt_COMPAY.Text = emp.COMPAY;
                txt_EMNAME.Text = emp.EMNAME;
                txt_EMPLID.Text = emp.EMPLID;
                txt_EMPLID.Enabled = false;
                txt_ENGNAM.Text = emp.ENGNAM;
                if (emp.GraduationTime.HasValue)
                    txt_GraduationTime.Text = emp.GraduationTime.Value.ToString("yyyy-MM-dd");
                txt_HomeTel.Text = emp.HomeTel;
                txt_Hometown.Text = emp.Hometown;
                txt_IDCard.Text = emp.IDCard;
                txt_JOBS.Text = emp.JOBS;
                txt_LinkPhone.Text = emp.LinkPhone;
                txt_Nation.Text = emp.Nation;
                txt_Phone.Text = emp.Phone;
                txt_REMARK.Text = emp.REMARK;
                txt_SHCOOL.Text = emp.SHCOOL;
                txt_Specialty.Text = emp.Specialty;
                txt_UEMAIL.Text = emp.UEMAIL;
                Drp_Degree.SelectedValue = emp.Degree;
                Drp_DEPMET.SelectedValue = emp.DEPMETID.Value.ToString();
                Drp_EDUCAT.SelectedValue = emp.EDUCAT;
                Drp_EMPSEX.SelectedValue = emp.EMPSEX;
                Drp_PoliticalType.SelectedValue = emp.PoliticalType;
                img_HeadLogo.ImageUrl = emp.HeadLog;
            }
        }

        protected bool ValidateForms()
        {
            bool f = true;
            string message = "";
            if (txt_EMPLID.Text == "")
            {
                message = "请输入员工编号的信息";
                Alert.Show(message);
                txt_EMPLID.MarkInvalid(message);
                return false;
            }
            else
            {
                HR_EMP em = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.EMPLID == txt_EMPLID.Text).ToFirst();
                if (em != null && string.IsNullOrEmpty(Request["empid"]))
                {
                    message = "员工编号已经存在";
                    Alert.Show(message);
                    txt_EMPLID.MarkInvalid(message);
                    return false;
                }

            }
            if (txt_EMNAME.Text == "")
            {
                message = "请输入员工姓名的信息";
                Alert.Show(message);
                txt_EMNAME.MarkInvalid(message);
                return false;
            }
            if (Drp_DEPMET.SelectedValue == "0")
            {
                message = "请选择所属部门";
                Alert.Show(message);
                Drp_DEPMET.MarkInvalid(message);
                return false;
            }
            if (Drp_EMPSEX.SelectedValue == "0")
            {
                message = "请选择员工性别";
                Alert.Show(message);
                Drp_EMPSEX.MarkInvalid(message);
                return false;
            }
            if (txt_JOBS.Text == "")
            {
                message = "请输入职称的信息";
                Alert.Show(message);
                txt_JOBS.MarkInvalid(message);
                return false;
            }
            if (txt_Phone.Text == "")
            {
                message = "请输入手机的信息";
                Alert.Show(message);
                txt_Phone.MarkInvalid(message);
                return false;
            }

            return f;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (ValidateForms())
            {

                int returnvalue = 0;
                string path = "";
                OA_Department dep = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == int.Parse(Drp_DEPMET.SelectedValue)).ToFirst();
                if (dep != null)
                {
                    path = dep.DepPath + dep.DepID + "|";
                }
                HR_EMP hm = new HR_EMP();
                if (!string.IsNullOrEmpty(Request["empid"]))
                {

                    hm.ID = int.Parse(Request["empid"]);
                    hm.Attach();
                    if (txt_BIRDAT.Text != "")
                        hm.BIRDAT = Convert.ToDateTime(txt_BIRDAT.Text);
                    hm.COMPAY = txt_COMPAY.Text;
                    hm.Degree = Drp_Degree.SelectedText;
                    //hm.DEPMET = txt_DEPMET.Text;
                    hm.DEPMET = Drp_DEPMET.SelectedText;
                    hm.DEPMETID = int.Parse(Drp_DEPMET.SelectedValue);
                    hm.EDUCAT = Drp_EDUCAT.SelectedText;
                    hm.EMNAME = txt_EMNAME.Text;
                    hm.EMPLID = txt_EMPLID.Text;
                    hm.EMPSEX = Drp_EMPSEX.SelectedText;
                    hm.ENGNAM = txt_ENGNAM.Text;
                    if (txt_GraduationTime.Text != "")
                        hm.GraduationTime = Convert.ToDateTime(txt_GraduationTime.Text);
                    hm.HeadLog = img_HeadLogo.ImageUrl;
                    hm.HomeTel = txt_HomeTel.Text;
                    hm.Hometown = txt_Hometown.Text;
                    hm.IDCard = txt_IDCard.Text;
                    hm.JOBS = txt_JOBS.Text;
                    hm.LinkPhone = txt_LinkPhone.Text;
                    hm.Nation = txt_Nation.Text;
                    hm.Phone = txt_Phone.Text;
                    hm.PoliticalType = Drp_PoliticalType.SelectedText;
                    hm.REMARK = txt_REMARK.Text;
                    hm.SHCOOL = txt_SHCOOL.Text;
                    hm.Specialty = txt_Specialty.Text;
                    hm.UEMAIL = txt_UEMAIL.Text;
                    hm.DepPath = path;
                    returnvalue = TbsDbSession.Default.Update<HR_EMP>(hm);
                }
                else
                {
                    hm.DeAttach();
                    if (txt_BIRDAT.Text != "")
                        hm.BIRDAT = Convert.ToDateTime(txt_BIRDAT.Text);
                    hm.COMPAY = txt_COMPAY.Text;
                    hm.Degree = Drp_Degree.SelectedText;
                    hm.DEPMET = Drp_DEPMET.SelectedText;
                    hm.DEPMETID = int.Parse(Drp_DEPMET.SelectedValue);
                    // hm.DEPMET = txt_DEPMET.Text;
                    hm.EDUCAT = Drp_EDUCAT.SelectedText;
                    hm.EMNAME = txt_EMNAME.Text;
                    hm.EMPLID = txt_EMPLID.Text;
                    hm.EMPSEX = Drp_EMPSEX.SelectedText;
                    hm.ENGNAM = txt_ENGNAM.Text;
                    if (txt_GraduationTime.Text != "")
                        hm.GraduationTime = Convert.ToDateTime(txt_GraduationTime.Text);
                    hm.HeadLog = img_HeadLogo.ImageUrl;
                    hm.HomeTel = txt_HomeTel.Text;
                    hm.Hometown = txt_Hometown.Text;
                    hm.IDCard = txt_IDCard.Text;
                    hm.JOBS = txt_JOBS.Text;
                    hm.LinkPhone = txt_LinkPhone.Text;
                    hm.Nation = txt_Nation.Text;
                    hm.Phone = txt_Phone.Text;
                    hm.PoliticalType = Drp_PoliticalType.SelectedText;
                    hm.REMARK = txt_REMARK.Text;
                    hm.SHCOOL = txt_SHCOOL.Text;
                    hm.Specialty = txt_Specialty.Text;
                    hm.UEMAIL = txt_UEMAIL.Text;
                    hm.DepPath = path;
                    returnvalue = TbsDbSession.Default.Insert<HR_EMP>(hm);
                    Sys_Users userinfo = new Sys_Users();
                    userinfo.DeAttach();
                    userinfo.AddTime = DateTime.Now;
                    userinfo.IsLock = false;
                    userinfo.Pwd = StringHelper.md5(txt_EMPLID.Text, 16);
                    userinfo.UserName = txt_EMPLID.Text;
                    TbsDbSession.Default.Insert<Sys_Users>(userinfo);
                }
                if (returnvalue > 0)
                {

                    Alert.Show("保存成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }

        protected void File_Upload_FileSelected(object sender, EventArgs e)
        {
            if (File_Upload.HasFile)
            {
                string fileName = File_Upload.ShortFileName;

                //if (!ValidateFileType(fileName))
                //{
                //    Alert.Show("无效的文件类型！");
                //    return;
                //}

                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                File_Upload.SaveAs(Server.MapPath("~/upload/" + fileName));

                img_HeadLogo.ImageUrl = "~/upload/" + fileName;
                // 清空文件上传组件
                File_Upload.Reset();
            }
        }

        protected void hibtn_Click(object sender, EventArgs e)
        {
            //if (hid1.Text == "1")
            //{
            //    Con1.Hidden = false;
            //    Con2.Hidden = true;
            //    Con3.Hidden = true;
            //}
            //if (hid1.Text == "2")
            //{
            //    Con1.Hidden = true;
            //    Con2.Hidden = false;
            //    Con3.Hidden = true;
            //}
            //if (hid1.Text == "3")
            //{
            //    Con1.Hidden = true;
            //    Con2.Hidden = true;
            //    Con3.Hidden = false;
            //}
        }
    }
}